package com.kingyea.mobilepolice.dictionary.controller;

import com.kingyea.mobilepolice.dictionary.bean.Dictionary;
import com.kingyea.mobilepolice.dictionary.bean.DictionaryCondition;
import com.kingyea.mobilepolice.dictionary.bean.DictionaryVo;
import com.kingyea.mobilepolice.dictionary.service.DictionaryService;
import com.kingyea.mobilepolice.orgjobcategory.bean.OrgDictionaryJobCategoryCondition;
import com.kingyea.mobilepolice.orgjobcategory.bean.OrgDictionaryJobCategoryPo;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.system.StateCode;
import com.kingyea.mobilepolice.util.BasePage;
import com.kingyea.mobilepolice.util.ExceptionConditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hx
 * @version v1.0.0
 * @description Dictionary 控制层
 * @date Created in 2018-04-12 16:42:54
 */
@RequestMapping(value = "/dictionary")
@RestController
public class DictionaryController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DictionaryController.class);

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @PostMapping("/list")
    public Result<BasePage<Dictionary>> list(@RequestBody DictionaryCondition condition) throws Exception {
        List<Dictionary> dictionaryList = dictionaryService.search(condition);
        condition.setList(dictionaryList);
        return new Result<>(condition);
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object detail(@PathVariable Integer id) throws Exception {
        return dictionaryService.getById(id);
    }

    /**
     * 创建
     *
     * @param dictionary
     * @return
     */
    @PostMapping("/save")
    public Object create(@RequestBody Dictionary dictionary) throws Exception {
        return dictionaryService.save(dictionary);
    }

    /**
     * 更新
     *
     * @param dictionary
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody Dictionary dictionary) throws Exception {
        return dictionaryService.update(dictionary);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id) throws Exception {
        DictionaryCondition condition = new DictionaryCondition();
        condition.setPid(id);
        List<Dictionary> list = dictionaryService.search(condition);
        if (list.size() > 0) {
            return -1;
        } else {
            return dictionaryService.deleteById(id);
        }
    }

    @PostMapping("/tree")
    public Result<List<DictionaryVo>> tree(@RequestBody DictionaryCondition condition) {
        return new Result<>(dictionaryService.searchTree(condition));
    }

    @PostMapping("/org/search")
    public Object orgSearch(@RequestBody OrgDictionaryJobCategoryCondition condition) {
//        ExceptionConditions.checkArrayNotEmpty(condition.getOrgSerialNos(), StateCode.NO_ARGUMENT_ERROR);
        DictionaryCondition dictionaryCondition = new DictionaryCondition();
        BeanUtils.copyProperties(condition, dictionaryCondition);
        dictionaryCondition.setList(dictionaryService.searchByOrgSerialNo(condition));
        dictionaryCondition.setTotal(condition.getTotal());
        return dictionaryCondition;
    }

    @PostMapping("/org/update")
    public Object orgUpdate(@RequestBody OrgDictionaryJobCategoryPo po) {
        ExceptionConditions.checkNotNull(po.getDistionaryIds(), StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkStringNotEmpty(po.getOrgSerialNo(), StateCode.NO_ARGUMENT_ERROR);
        return dictionaryService.updateForOrg(po);
    }
}
